﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace UI
{
    public partial class QuanLyGioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["TenDN"] != null)
                {
                    Loaddulieu();
                }
                else
                {
                    MultiView1.ActiveViewIndex = 1;
                }
            }
        }
        private void Loaddulieu()
        {
            MultiView1.ActiveViewIndex = 0;
            GridView1.PageSize = 5;
            DataTable dt = (DataTable)Session["GioHang"];
            if (dt == null)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
                return;


            }
            else if (dt.Rows.Count > 0)
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                tinhtien(dt);
            }
            else
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            tinhtien(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["soluong"].ToString() == "0")
                    delete(i);

            }
        }
        private void tinhtien(DataTable dt)
        {
            double tien = 0;
            if (Session["GioHang"] != null)
            {
                DataTable dt1 = (DataTable)Session["GioHang"];
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    tien = tien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());

                }
                lbtongtien.Text = "Tổng giá trị đơn hàng: " + tien.ToString();
            }
            else
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
        }
        void delete(int row)
        {
            DataTable dt = (DataTable)Session["GioHang"];
            dt.Rows[row].Delete();
            Session["GioHang"] = dt;

            Response.Redirect("GioHang.aspx");
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int row = e.RowIndex;
            DataTable dt = (DataTable)Session["GioHang"];
            dt.Rows[row].Delete();
            Session["GioHang"] = dt;
            GridView1.EditIndex = -1;

            Response.Redirect("QuanLyGioHang.aspx");
        }
        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Loaddulieu();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Loaddulieu();
        }


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Loaddulieu();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox soLuong = GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            int row = e.RowIndex;
            DataTable dt = (DataTable)Session["GioHang"];
            dt.Rows[row][4] = int.Parse(soLuong.Text);
            Session["GioHang"] = dt;
            GridView1.EditIndex = -1;
            //   Loaddulieu();
            Response.Redirect("QuanLyGioHang.aspx");



        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Loaddulieu();
        }

        protected void btnmuatiep_Click1(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }

        protected void btnxoatoanbo_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)Session["GioHang"];
            dt.Rows.Clear();
            Session["GioHang"] = dt;
            Response.Redirect("giohangcuaban.aspx");
        }

        protected void btnguidon_Click(object sender, EventArgs e)
        {
            Response.Redirect("thanhtoan.aspx");
        }
    }
}